//
//  AppContext.swift
//  BareFootDoctor
//
//  Created by HippieFox on 2019/4/25.
//  Copyright © 2019 Stack. All rights reserved.
/*
 * App 运行的Context
 */

import UIKit


/// 测试环境API
let BASE_URL_DEV = "http://zt.panda.mofyi.com"

/// 生产环境
let BASE_URL_PRODUCTION = "http://zt.panda.mofyi.com"

private let APP_USER_TOKEN = {
    return "::APP::USER::TOKEN"
}()

private let APP_USER_MERCHANT = {
    return "::APP::USER::MERCHANT"
}()


private let APP_USER_ID = {
    return "::APP::USER::ID"
}()



private let  bannerPlaceHolderImageName = "BBS_detail_placeholder"
private let  imgPlaceHolderImageName = "BBS_placeholder"
private let  themeColorStr = "#FFA830"


class AppContext : NSObject{
    @objc var member_id:String?
    @objc var merchant:String?
    @objc var access_token:String?
    

    var isLogin:Bool = false{
        didSet{
            if self.isLogin == true{
                self.synchronizeLoginInfo()
            }else{
                self.cleatLoginInfo()
            }
        }
    }
    var email:String? //用户登录的email
    var pwd:String? //登录的密码
    

    
    
//    static let sharedInstance = AppContext()

    @objc static let shared:AppContext = AppContext()
    



    
    @objc func queryLocalInfo(){
        self.merchant = UserDefaults.standard.object(forKey:APP_USER_MERCHANT) as? String
        self.access_token = UserDefaults.standard.object(forKey:APP_USER_TOKEN) as? String
        self.member_id = UserDefaults.standard.object(forKey:APP_USER_ID) as? String

    }
    
    
    @objc func cleatLoginInfo(){
        UserDefaults.standard.set(nil, forKey: APP_USER_ID)
        UserDefaults.standard.set(nil, forKey: APP_USER_TOKEN)
        UserDefaults.standard.set(nil, forKey: APP_USER_MERCHANT)

    }
    
    
    @objc func synchronizeLoginInfo(){
        if let merchant = self.merchant, let member_id = self.member_id,let access_token = self.access_token{
            UserDefaults.standard.set(merchant, forKey: APP_USER_MERCHANT)
            UserDefaults.standard.set(member_id, forKey: APP_USER_ID)
            UserDefaults.standard.set(access_token, forKey: APP_USER_TOKEN)

        }
    }
    
    
  
}

